<template>
    <el-button type="primary" @click="dialogFormVisible=true">添加用户</el-button>
    
  <el-table :data="tableData" stripe style="width: 100%">
    <el-table-column prop="id" label="Id" width="180" />
    <el-table-column prop="uid" label="Uid" width="180" />
    <el-table-column prop="phone" label="Phone" />
    <el-table-column prop="nickname" label="Nickname" />
    <el-table-column prop="image" label="Image" >
        <template #default="scope">
            <img :src="scope.row.image ? scope.row.image : defaultAvatar" min-width="70"height="70"/>
        </template>
    </el-table-column>
    
    <el-table-column fixed="right" label="Operations" min-width="120">
      <template #default="scope">
        <el-button   size="small" @click="editUser(scope.$index, scope.row)">
          编辑
        </el-button>
        <el-button   size="small" @click="deleteUser(scope.$index, scope.row)">删除</el-button>
      </template>
    </el-table-column>
  </el-table>
  <el-dialog v-model="dialogFormVisible" title="添加管理员" width="500">
    <el-form :model="form">
      <el-form-item label="手机号" :label-width="formLabelWidth">
        <el-input v-model="form.phone" autocomplete="off" />
      </el-form-item>
      <el-form-item label="密码" :label-width="formLabelWidth">
        <el-input v-model="form.password" autocomplete="off" />
      </el-form-item>
      <el-form-item label="昵称" :label-width="formLabelWidth">
        <el-input v-model="form.nickname" autocomplete="off" />
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取消</el-button>
        <el-button type="primary" @click="confirmAddUser">
          确定
        </el-button>
      </div>
    </template>
  </el-dialog>
  <el-dialog v-model="dialogFormEditVisible" title="编辑管理员" width="500">
    <el-form :model="form">
      <el-form-item label="手机号" :label-width="formLabelWidth">
        <el-input v-model="form.phone" autocomplete="off" />
      </el-form-item>
      <el-form-item label="密码" :label-width="formLabelWidth">
        <el-input v-model="form.password" autocomplete="off" />
      </el-form-item>
      <el-form-item label="昵称" :label-width="formLabelWidth">
        <el-input v-model="form.nickname" autocomplete="off" />
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="dialogFormEditVisible = false">取消</el-button>
        <el-button type="primary" @click="confirmEditUser">
          确定
        </el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup lang="js">
import { onMounted, ref } from 'vue'
import { reactive } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import request from '@/util/request';
const defaultAvatar="https://cube.elemecdn.com/0/88/03b0d39583f48206768a7534e55bcpng.png"
const tableData = ref([])
const dialogFormVisible = ref(false)
const dialogFormEditVisible = ref(false)

const formLabelWidth = '140px'
const form = reactive({
  phone: '',
  password: '',
  nickname: '',
  id: '',
})
// const tableData = [
//     {
//         id: 1,
//         uid:"admin_12321",
//         phone:"123456789",
//         nickname:"admin",
//         image:"https://cube.elemecdn.com/0/88/03b0d39583f48206768a7534e55bcpng.png"
//     }
// ]
onMounted(() => {
    request.post('/super/admin/list/').then(res => {
        // console.log(res.data.data)
        tableData.value = res.data.data
    })
})
 // 处理行点击事件的方法  
 const editUser = (index, row) => {  
    dialogFormEditVisible.value = true
    console.log(row);
    
    form.phone = row.phone
    form.password = row.password
    form.nickname = row.nickname
    form.id = row.id
    
      // 你可以在这里根据row.id进行其他操作  
    }; 
 // 处理行点击事件的方法  
 const deleteUser = (index, row) => {  
  ElMessageBox.confirm(
    '确定删除？',
    'Warning',
    {
      confirmButtonText: 'OK',
      cancelButtonText: 'Cancel',
      type: 'warning',
    }
  )
    .then(async () => {
      const id = row.id; // 确保 ID 正确
      console.log('Deleting ID:', id);

      try {
        const res = await request.post('/super/admin/delete/', { id });
        if (res.data.code==0) { // 根据后端返回的字段判断是否成功
          tableData.value.splice(index, 1);
          ElMessage({
            type: 'success',
            message: '删除成功',
          });
        } else {
          ElMessage({
            type: 'error',
            message: res.data.message || '删除失败',
          });
        }
      } catch (error) {
        console.error('Delete failed:', error);
        ElMessage({
          type: 'error',
          message: 'Delete request failed',
        });
      }
    })
    .catch(() => {
      ElMessage({
        type: 'info',
        message: 'Delete canceled',
      });
    });
};

const confirmAddUser = async() => {
    await request.post('/super/admin/add/', {
        phone: form.phone,
        password: form.password,
        nickname: form.nickname,
        image: "https://cube.elemecdn.com/0/88/03b0d39583f48206768a7534e55bcpng.png"
    }).then(res => {
        console.log(res.data);
        dialogFormVisible.value = false
        form.phone = ''
        form.password = ''
        form.nickname = ''
        request.post('/super/admin/list/').then(res => {
            console.log(res.data.data)
            tableData.value = res.data.data
        })
        // window.location.reload()
    })
}
const confirmEditUser = async() => {
    await request.post('/super/admin/edit/', {
        id: form.id,
        phone: form.phone,
        password: form.password,
        nickname: form.nickname,
    }).then(res => {
        console.log(res.data);
        dialogFormEditVisible.value = false
        form.phone = ''
        form.password = ''
        form.nickname = ''
        request.post('/super/admin/list/').then(res => {
            console.log(res.data.data)
            tableData.value = res.data.data
        })
    })
}
</script>

<style scoped lang="less">

</style>